package com.tengfei.blogsystem.service.impl;

import com.tengfei.blogsystem.dao.SettingsDao;
import com.tengfei.blogsystem.pojo.Settings;
import com.tengfei.blogsystem.response.ResponseResult;
import com.tengfei.blogsystem.response.ResponseState;
import com.tengfei.blogsystem.service.IWebSiteInfoService;
import com.tengfei.blogsystem.utils.Constants;
import com.tengfei.blogsystem.utils.SnowFlakeIdWorker;
import com.tengfei.blogsystem.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-28-16:55
 * @description
 */
@Service
@Transactional
public class WebSiteInfoServiceImpl implements IWebSiteInfoService {

    @Autowired
    private SnowFlakeIdWorker idWorker;
    @Autowired
    private SettingsDao settingsDao;

    @Override
    public ResponseResult getWebTitle() {
        Settings settingByKey = settingsDao.findOneByKey(Constants.Settings.WEB_SITE_TITLE);
        return new ResponseResult(ResponseState.SUCCESS, "获取网站标题成功", settingByKey);
    }

    @Override
    public ResponseResult updateWebTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            return new ResponseResult(ResponseState.ERROR, "网站标题不能为空");
        }
        Settings settingByKey = settingsDao.findOneByKey(Constants.Settings.WEB_SITE_TITLE);
        if (settingByKey == null) {
            settingByKey = new Settings();
            settingByKey.setId(String.valueOf(idWorker.nextId()));
            settingByKey.setCreateTime(new Date());
            settingByKey.setKey(Constants.Settings.WEB_SITE_TITLE);
        }
        settingByKey.setValue(title);
        settingByKey.setUpdateTime(new Date());
        settingsDao.save(settingByKey);
        return new ResponseResult(ResponseState.SUCCESS, "更新网站标题成功");
    }

    @Override
    public ResponseResult getWebSeo() {
        Settings settingByDescriptions = settingsDao.findOneByKey(Constants.Settings.WEB_SITE_DESCRIPTION);
        Settings settingByKeywords = settingsDao.findOneByKey(Constants.Settings.WEB_SITE_KEYWORDS);
        Map<String, Object> map = new HashMap<>();
        map.put(settingByDescriptions.getKey(), settingByDescriptions);
        map.put(settingByKeywords.getKey(), settingByKeywords);
        return new ResponseResult(ResponseState.SUCCESS, "获取网站seo成功", map);
    }

    @Override
    public ResponseResult updateWebSeo(String keywords, String description) {
        if (TextUtils.isEmpty(description)) {
            return new ResponseResult(ResponseState.ERROR, "网站描述不能为空");
        }
        if (TextUtils.isEmpty(keywords)) {
            return new ResponseResult(ResponseState.ERROR, "网站关键字不能为空");
        }
        Settings settingByDescriptions = settingsDao.findOneByKey(Constants.Settings.WEB_SITE_DESCRIPTION);
        Settings settingByKeywords = settingsDao.findOneByKey(Constants.Settings.WEB_SITE_KEYWORDS);
        if (settingByDescriptions == null) {
            settingByDescriptions = new Settings();
            settingByDescriptions.setId(String.valueOf(idWorker.nextId()));
            settingByDescriptions.setCreateTime(new Date());
            settingByDescriptions.setKey(Constants.Settings.WEB_SITE_DESCRIPTION);
        }
        if (settingByKeywords == null) {
            settingByKeywords = new Settings();
            settingByKeywords.setId(String.valueOf(idWorker.nextId()));
            settingByKeywords.setCreateTime(new Date());
            settingByKeywords.setKey(Constants.Settings.WEB_SITE_KEYWORDS);
        }
        settingByDescriptions.setUpdateTime(new Date());
        settingByDescriptions.setValue(description);
        settingByKeywords.setUpdateTime(new Date());
        settingByKeywords.setValue(keywords);
        settingsDao.save(settingByDescriptions);
        settingsDao.save(settingByKeywords);
        return new ResponseResult(ResponseState.SUCCESS, "更新网站seo信息成功");
    }

    @Override
    public ResponseResult getWebViewCount() {
        Settings settingByViewCount = settingsDao.findOneByKey(Constants.Settings.WEB_SITE_VIEW_COUNT);
        if (settingByViewCount == null) {
            settingByViewCount = new Settings();
            settingByViewCount.setId(String.valueOf(idWorker.nextId()));
            settingByViewCount.setCreateTime(new Date());
            settingByViewCount.setKey(Constants.Settings.WEB_SITE_VIEW_COUNT);
            settingByViewCount.setValue("1");
        }
        settingByViewCount.setUpdateTime(new Date());
        return new ResponseResult(ResponseState.SUCCESS, "获取网站浏览量成功", settingByViewCount);
    }
}
